<?php

function include_dir($path,$read=false) {
	$s = DIRECTORY_SEPARATOR;
	//abrimos el directorio
	if ($handle = @opendir($path)) {
		while (false !== ($file = readdir($handle))) { 
			if ($file != "." && $file != "..") { 
				//si es un directorio lo recorremos en caso de activar la recursividad
				if(is_dir($path.$s.$file) and $read) {
					include_dir($path.$s.$file,true);
				} else {
					$ext = substr(strtolower($file),-3);
					if($ext == 'php') include_once($path.$s.$file);
				}
			} 
        }
        //cerramos el directorio
        closedir($handle); 
	}
}

function routes($default_controller) {
	global $htaccess;
	$htaccess = array();
	$path = @$_SERVER['PATH_INFO'];
	
	$arguments = explode('/',$path);
	array_shift($arguments);
	$end = array_pop($arguments);
	$end = array_pop($arguments);
	
	$format = substr(strrchr($end, '.'),1);
	if($format) $arguments[] = substr($end,0,strrpos($end, '.'));
	else $arguments[] = $end;

	@$arguments[0] = @$arguments[0] ? $arguments[0] : $default_controller;
	
	@$arguments[1] = @$arguments[1] ? $arguments[1] : 'index';

	if(is_numeric(@$arguments[1])) {
		$n = $arguments[1];
		if(@$arguments[2]) {
			$arguments[1] = $arguments[2].'_numeric';
			$arguments[2] = $n;
		} else {
			$arguments[1] = 'index_numeric';
			$arguments[2] = $n;
		}
	}
	$htaccess['args'] = $arguments;
	$i = 0;
	foreach($htaccess['args'] as $argument) {
		$htaccess['value'][$argument] = @$htaccess['args'][++$i];
	}
	
	$controller = str_replace('-', '_', strtolower($arguments[0]));
	$action = str_replace('-', '_', strtolower($arguments[1]));
	
	return array(
		'controller' => $controller,
		'action' => $action,
		'format' => $format ? strtolower($format) : 'php'
	);
}

function is_update_page($update_ips,$update_page) {
	if($update_page && !in_array(get_ip(),$update_ips)) {
		include PBC.'update.php';
		die();
	}
}

function get_ip() {
	if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	elseif(isset($_SERVER['HTTP_VIA'])) $ip = $_SERVER['HTTP_VIA'];
	elseif(isset($_SERVER['REMOTE_ADDR'])) $ip = $_SERVER['REMOTE_ADDR'];
	else $ip = false;
	return $ip;
}

function public_actions($action) {
	if($action[0] == '_' ) return false;
	if(in_array($action, array('before_action','after_action'))) return false;
	return true;
}

function error_404($type= 0, $error='') {
	$code = 404;
	header('HTTP/1.0 404 Not Found');
	header('Content-Type: text/html; charset=utf-8');
	include PBC.'error.php';
	die();
}

function error_500($type= 0, $error='') {
	$code = 500;
	header('HTTP/1.0 500 Internal Server Error');
	header('Content-Type: text/html; charset=utf-8');
	include PBC.'error.php';
	die();
}

function content_type($type = 'text/html') {
	global $content_type;
	$content_type = $type;
	if($type) header('Content-Type: '.$content_type.'; charset=utf-8');
}

function url($arg=0) {
	global $htaccess;
	return is_numeric($arg) ? @$htaccess['args'][$arg+2] : @$htaccess['value'][$arg];
}

function _view($view) {
	$_view = VIEWS.CONTROLLER.DS.$view.'.php';
	if(file_exists($_view)) return $_view;
	elseif(file_exists(VIEWS.$view.'.php')) return VIEWS.$view.'.php';
	error_404(5,'View <strong>'.$view.'</strong> not found');
}

function view() {
	$views = func_get_args();
	$views = array_map('_view',$views);
	return $views;
}

function f($file='') {
	return @file_get_contents($file);
}

function save_file($file,$data,$type="w") {
	$fp = @fopen($file,$type);
    if(!$fp) return false;
    fwrite($fp,$data);
    fclose($fp);
    return true;
}

function print_d($array){
	echo "<pre>\n";
	print_r($array);
	echo "</pre>\n";
}

?>